package jh.com.jhappbf.dispatchcar;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.util.AbDialogUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.ab.view.wheel.AbWheelUtil;
import com.ab.view.wheel.AbWheelView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;
import jh.com.jhappbf.model.ContractSettingModel;
import jh.com.jhappbf.valueselect.ContractValueSelect;

/**
 * @author Administrator
 */
public class ContractSelectSetting extends AbActivity implements OnClickListener {
    private TextView mTxtStartDate;
    private TextView mTxtStartTime;
    private TextView mTxtEndDate;
    private TextView mTxtEndTime;
    private TextView mTxtSpec;
    private TextView mTxtCargoname;
    private TextView mTxtSender;
    private TextView mTxtReceiver;
    private Switch mSwitchStates;

    private Button mBtnStartDate;
    private Button mBtnStartTime;
    private Button mBtnEndDate;
    private Button mBtnEndTime;
    private Button mBtnCompany;
    private Button mBtnCargoname;
    private Button mBtnSender;
    private Button mBtnReceiver;

    private View mWheelDate;
    private final int GETSPEC = 0x0001;
    private final int GETCARGONAME = 0X0002;
    private final int GETSENDER = 0X0003;
    private final int GETRECEIVER = 0X0004;
    private String mPlanStates = "0";
    private String mType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.contract_select_setting);
        mType=getIntent().getStringExtra("mType");
        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("合同选择设置");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        mAbTitleBar.setLogoOnClickListener(v -> saveSettings());
        initView();
    }

    private void saveSettings() {
        String startDate = mTxtStartDate.getText().toString().trim();
        String startTime = mTxtStartTime.getText().toString().trim();
        String endDate = mTxtEndDate.getText().toString().trim();
        String endTime = mTxtEndTime.getText().toString().trim();
        String spec = mTxtSpec.getText().toString().trim();
        String cargoName = mTxtCargoname.getText().toString().trim();
        String sender = mTxtSender.getText().toString().trim();
        String receiver = mTxtReceiver.getText().toString().trim();

        ContractSettingModel model = new ContractSettingModel();
        if (startDate.length() != 0 || startTime.length() != 0
                || endDate.length() != 0 || endTime.length() != 0
                || spec.length() != 0 || cargoName.length() != 0
                || sender.length() != 0 || receiver.length() != 0) {

            if (startDate.length() > 0 && startTime.length() > 0) {
                model.setStartTime(startDate + " " + startTime);
            }

            if (endDate.length() > 0 && endTime.length() > 0) {
                model.setEndTime(endDate + " " + endTime);
            }

            if (spec.length() > 0) {
                model.setSpec(spec);
            }

            if (cargoName.length() > 0) {
                model.setCargoname(cargoName);
            }

            if (sender.length() > 0) {
                model.setSender(sender);
            }

            if (receiver.length() > 0) {
                model.setReceiver(receiver);
            }

            model.setStates(mPlanStates);

        } else {
            model = null;
        }

        Intent i = new Intent();
        i.putExtra("result", model);
        setResult(RESULT_OK, i);
        ContractSelectSetting.this.finish();
    }

    /**
     * 初始化
     */
    private void initView() {
        mTxtStartDate = findViewById(R.id.contract_select_setting_start_date_txt);
        mTxtStartTime = findViewById(R.id.contract_select_setting_start_time_txt);
        mTxtEndDate = findViewById(R.id.contract_select_setting_end_date_txt);
        mTxtEndTime = findViewById(R.id.contract_select_setting_end_time_txt);
        mTxtSpec = findViewById(R.id.contract_select_setting_spec_txt);
        mTxtCargoname = findViewById(R.id.contract_select_setting_cargoname_txt);
        mTxtSender = findViewById(R.id.contract_select_setting_sender_txt);
        mTxtReceiver = findViewById(R.id.contract_select_setting_receiver_txt);
        mSwitchStates = findViewById(R.id.contract_plan_states);

        mBtnStartDate = findViewById(R.id.contract_select_setting_start_date_btn);
        mBtnStartTime = findViewById(R.id.contract_select_setting_start_time_btn);
        mBtnEndDate = findViewById(R.id.contract_select_setting_end_date_btn);
        mBtnEndTime = findViewById(R.id.contract_select_setting_end_time_btn);
        mBtnCompany = findViewById(R.id.contract_select_setting_spec_btn);
        mBtnCargoname = findViewById(R.id.contract_select_setting_cargoname_btn);
        mBtnSender = findViewById(R.id.contract_select_setting_sender_btn);
        mBtnReceiver = findViewById(R.id.contract_select_setting_receiver_btn);

        mTxtStartDate.setOnClickListener(this);
        mTxtStartTime.setOnClickListener(this);
        mTxtEndDate.setOnClickListener(this);
        mTxtEndTime.setOnClickListener(this);
        mTxtSpec.setOnClickListener(this);
        mTxtCargoname.setOnClickListener(this);
        mTxtSender.setOnClickListener(this);
        mTxtReceiver.setOnClickListener(this);

        mBtnStartDate.setOnClickListener(this);
        mBtnStartTime.setOnClickListener(this);
        mBtnEndDate.setOnClickListener(this);
        mBtnEndTime.setOnClickListener(this);
        mBtnCompany.setOnClickListener(this);
        mBtnCargoname.setOnClickListener(this);
        mBtnSender.setOnClickListener(this);
        mBtnReceiver.setOnClickListener(this);

        mSwitchStates.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mPlanStates = "1";
            } else {
                mPlanStates = "0";
            }
        });

        Intent i = getIntent();
        ContractSettingModel model = i.getParcelableExtra("setting");
        if (model != null) {
            String start = model.getStartTime();
            String end = model.getEndTime();
            String spec = model.getSpec();
            String cargoname = model.getCargoname();
            String sender = model.getSender();
            String receiver = model.getReceiver();
            String states = model.getStates();

            if (start.length() > 0) {
                String[] arrStrings = start.split(" ");
                if (arrStrings.length > 0) {
                    mTxtStartDate.setText(arrStrings[0]);
                }
                if (arrStrings.length > 1) {
                    mTxtStartTime.setText(arrStrings[1]);
                }
            }

            if (end.length() > 0) {
                String[] arrStrings = end.split(" ");
                if (arrStrings.length > 0) {
                    mTxtEndDate.setText(arrStrings[0]);
                }
                if (arrStrings.length > 1) {
                    mTxtEndTime.setText(arrStrings[1]);
                }
            }

            if (spec.length() > 0 && !"000".equals(spec)) {
                mTxtSpec.setText(spec);
            }

            if (cargoname.length() > 0) {
                mTxtCargoname.setText(cargoname);
            }

            if (sender.length() > 0) {
                mTxtSender.setText(sender);
            }

            if (receiver.length() > 0) {
                mTxtReceiver.setText(receiver);
            }

            if ("1".equals(states)) {
                mSwitchStates.setChecked(true);
            }
        } else {
            Date date = new Date();
            SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm", Locale.getDefault());
            mTxtEndDate.setText(formatDate.format(date));
            mTxtEndTime.setText(formatTime.format(date));
            mTxtStartTime.setText(formatTime.format(date));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, -30);
            Date yesterday = calendar.getTime();
            mTxtStartDate.setText(formatDate.format(yesterday));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.contract_select_setting_start_date_txt:
                initWheelThree(v);
                break;
            case R.id.contract_select_setting_start_time_txt:
                initWheelThreeTime(v);
                break;
            case R.id.contract_select_setting_end_date_txt:
                initWheelThree(v);
                break;
            case R.id.contract_select_setting_end_time_txt:
                initWheelThreeTime(v);
                break;
            case R.id.contract_select_setting_spec_txt:
                getFieldValue("单位", GETSPEC);
                break;
            case R.id.contract_select_setting_cargoname_txt:
                getFieldValue("货名", GETCARGONAME);
                break;
            case R.id.contract_select_setting_sender_txt:
                getFieldValue("发货", GETSENDER);
                break;
            case R.id.contract_select_setting_receiver_txt:
                getFieldValue("收货", GETRECEIVER);
                break;
            case R.id.contract_select_setting_start_date_btn:
                mTxtStartDate.setText("");
                break;
            case R.id.contract_select_setting_start_time_btn:
                mTxtStartTime.setText("");
                break;
            case R.id.contract_select_setting_end_date_btn:
                mTxtEndDate.setText("");
                break;
            case R.id.contract_select_setting_end_time_btn:
                mTxtEndTime.setText("");
                break;
            case R.id.contract_select_setting_spec_btn:
                mTxtSpec.setText("");
                break;
            case R.id.contract_select_setting_sender_btn:
                mTxtSender.setText("");
                break;
            case R.id.contract_select_setting_receiver_btn:
                mTxtReceiver.setText("");
                break;
            case R.id.contract_select_setting_cargoname_btn:
                mTxtCargoname.setText("");
                break;

            default:
                break;
        }
    }

    /**
     * 设置日期
     */
    private void initWheelThree(View txtDate) {
        mWheelDate = mInflater.inflate(R.layout.wheel_three_activity, null);
        initWheelDate(mWheelDate, (TextView) txtDate, "yyyy-MM-dd");
        AbDialogUtil.showDialog(mWheelDate, Gravity.BOTTOM);
    }

    /**
     * 设置时间
     */
    private void initWheelThreeTime(View txtTime) {
        mWheelDate = mInflater.inflate(R.layout.wheel_three_activity, null);
        initWheelTime(mWheelDate, (TextView) txtTime);
        AbDialogUtil.showDialog(mWheelDate, Gravity.BOTTOM);
    }

    // /获取字段的值
    private void getFieldValue(String flag, int requestCode) {
        Intent i = new Intent(ContractSelectSetting.this, ContractValueSelect.class);
        i.putExtra("flag", flag);
        i.putExtra("mType",mType);
        startActivityForResult(i, requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            String value = data.getStringExtra("result");
            if (requestCode == GETSPEC) {
                mTxtSpec.setText(value);
            } else if (requestCode == GETCARGONAME) {
                mTxtCargoname.setText(value);
            } else if (requestCode == GETSENDER) {
                mTxtSender.setText(value);
            } else if (requestCode == GETRECEIVER) {
                mTxtReceiver.setText(value);
            }
        }
    }

    /**
     * 初始化日期
     */
    public void initWheelDate(View mDateView, TextView mText, String flag) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        final AbWheelView mWheelViewY = mDateView.findViewById(R.id.wheelView1);
        final AbWheelView mWheelViewM = mDateView.findViewById(R.id.wheelView2);
        final AbWheelView mWheelViewD = mDateView.findViewById(R.id.wheelView3);
        if ("yyyy-MM".equals(flag)) {
            mWheelViewD.setVisibility(View.GONE);
        } else if ("yyyy".equals(flag)) {
            mWheelViewD.setVisibility(View.GONE);
            mWheelViewM.setVisibility(View.GONE);
        }
        Button okBtn = mDateView.findViewById(R.id.okBtn);
        Button cancelBtn = mDateView.findViewById(R.id.cancelBtn);
        mWheelViewY.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        mWheelViewM.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        mWheelViewD.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        AbWheelUtil.initWheelDatePicker(this, mText, mWheelViewY, mWheelViewM,
                mWheelViewD, okBtn, cancelBtn, year, month, day, 2000, 30,
                true, flag);
    }

    /**
     * 初始化时间
     */
    public void initWheelTime(View mDateView, TextView mText) {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE) + 1;
        final AbWheelView mWheelViewH = mDateView.findViewById(R.id.wheelView1);
        mWheelViewH.setVisibility(View.GONE);
        final AbWheelView mWheelViewM = mDateView.findViewById(R.id.wheelView2);
        final AbWheelView mWheelViewS = mDateView.findViewById(R.id.wheelView3);

        Button okBtn = mDateView.findViewById(R.id.okBtn);
        Button cancelBtn = mDateView.findViewById(R.id.cancelBtn);
        mWheelViewM.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        mWheelViewS.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        AbWheelUtil.initWheelTimePicker2(this, mText, mWheelViewM, mWheelViewS,
                okBtn, cancelBtn, hour, minute, true);
    }

    @Override
    public void onBackPressed() {
        saveSettings();
    }
}
